﻿namespace SRM
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Data;
    using SRM.Entities;
    using System.Linq;
    using System.Data.Linq;


    public class Help
    {
        public static DataTable GetSRMCode(string codeType, bool isSapCode)
        {
            System.Data.DataSet ds = new System.Data.DataSet();
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(
                System.Configuration.ConfigurationManager.ConnectionStrings["SRM"].ConnectionString);
            System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);

            cmd.CommandText = string.Format("SELECT * FROM SRM_Code WHERE CodeType LIKE '{0}' AND IsSAPCode = {1}",
                codeType.Trim(),
                isSapCode ? 1 : 0);

            da.Fill(ds, "SRM_Code");

            return ds.Tables["SRM_Code"];
        }

        public static bool GetIsSapRecord(DataRow dr)
        {
            return false;
            //if (drApplicant["CreatedDate"] != null)
            //{
            //    if (!drApplicant.IsNull("CreatedDate"))
            //    {

            //    }
            //    //DateTime RecordCreationDate = drApplicant["CreatedDate"] as DateTime?;

            //}

            //If (hdn_RECORD_MODIFIED_DATE.Value = "") Then
            //    USE_SAP_FORM = True
            //Else
            //    RECORD_MODIFIED_DATE = DateTime.ParseExact(hdn_RECORD_MODIFIED_DATE.Value, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture)
            //    SAP_START_DATE = DateTime.ParseExact(System.Configuration.ConfigurationManager.AppSettings("SAP_START_DATE"), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture)
            //    USE_SAP_FORM = RECORD_MODIFIED_DATE > SAP_START_DATE
            //End If
        }

        public static DataTable GetApplicant(string applicantKey)
        {
            System.Data.DataSet ds = new System.Data.DataSet();
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(
                System.Configuration.ConfigurationManager.ConnectionStrings["SRM"].ConnectionString);
            System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);

            cmd.CommandText = "SELECT * FROM SRM_Applicant_Tbl where ApplicantKey = " + applicantKey.Trim();

            da.Fill(ds, "Applicant");

            return ds.Tables["Applicant"];
        }

        public static DataTable GetFamilyMemberDataTable(string applicantKey)
        {
            System.Data.DataSet ds = new System.Data.DataSet();
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(
                System.Configuration.ConfigurationManager.ConnectionStrings["SRM"].ConnectionString);
            System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);

            cmd.CommandText = "SELECT * FROM SRM_ApplicantFamilyMembers_Tbl WHERE ApplicantKey = " + applicantKey.Trim();

            da.Fill(ds, "FamilyParticulars");

            DataTable dt = ds.Tables["FamilyParticulars"];
            
            return ds.Tables["FamilyParticulars"];
        }

        public static List<FamilyMember> GetFamilyMember(string applicantKey)
        {
            List<FamilyMember> result = null;
            DataTable dt = GetFamilyMemberDataTable(applicantKey);

            if (dt != null)
            {
                result = dt.AsEnumerable().Select(r => new FamilyMember
                {
                    FamilyMemberKey = r.Field<long>("FamilyMemberKey"),
                    ApplicantKey = r.Field<long>("FamilyMemberKey"),
                    MemberName = r.Field<string>("MemberName"),
                    Relationship = r.Field<string>("Relationship"),
                    BirthDate = r.Field<DateTime?>("BirthDate"),
                    Nationality = r.Field<string>("Nationality"),


                    FamilyEmployer = r.Field<string>("FamilyEmployer"),
                    Occupation = r.Field<string>("Occupation"),
                    EmergencyContact = r.Field<string>("EmergencyContact"),
                    ContactNo = r.Field<string>("ContactNo"),
                    Timestamp = r.Field<DateTime?>("Timestamp"),
                    DateOfMarriage = r.Field<DateTime?>("DateOfMarriage"),
                }).ToList();
            }

            return result;
        }

        public static string ParsedObjectToGetString(object o)
        {
            if (o == null)
                return string.Empty;

            return o.ToString();
        }

        public static DateTime? ParsedObjectToGetNullDateTime(object o)
        {
            DateTime? result = null;

            if (o == null)
                return result;

            result = DateTime.Parse(o.ToString());
            return result;
        }
        
    }
}
